package com.mentaleye.biz.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.DormitoryStudentBean;
import com.mentaleye.bean.DormitorysBean;
import com.mentaleye.biz.DormitoryStudentBiz;
import com.mentaleye.dal.model.DormitoryStudentExample;
import com.mentaleye.enums.CommonStatus;
import com.mentaleye.service.DormitoryService;
import com.mentaleye.service.DormitoryStudentService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;

@Service
public class DormitoryStudentBizImpl implements DormitoryStudentBiz {
	@Autowired
	private DormitoryStudentService dormitoryStudentService;
	@Autowired DormitoryService dormitoryService;
	private static Logger logger = Logger.getLogger(DormitoryStudentBizImpl.class);

	@Override
	public CommonResult editDormitoryStudent(DormitoryStudentBean dormitoryStudentBean) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			if (!dormitoryStudentService.isExistRelate(dormitoryStudentBean.getDorId(),
					dormitoryStudentBean.getStuId())) {
				success = dormitoryStudentService.addDormitoryStudent(dormitoryStudentBean);
				updateRestBed(dormitoryStudentBean);
			} else {
				if(dormitoryStudentBean.getStayStatus()==CommonStatus.HAS_CHECK_IN.getCode()){//已入住
					dormitoryStudentBean.setStayStatus(CommonStatus.HAS_CHECK_IN.getCode());
					dormitoryStudentBean.setMoveOutDate(null);
				}else if(dormitoryStudentBean.getStayStatus()==CommonStatus.HAS_MOVE_OUT.getCode()){
					dormitoryStudentBean.setStayStatus(CommonStatus.HAS_MOVE_OUT.getCode());
				}else if(dormitoryStudentBean.getStayStatus()==CommonStatus.NOT_CHECKIN.getCode()){
					dormitoryStudentBean.setStayStatus(CommonStatus.NOT_CHECKIN.getCode());
					dormitoryStudentBean.setMoveOutDate(null);
					dormitoryStudentBean.setCheckInDate(null);
				}
				success = dormitoryStudentService.editDormitoryStudent(dormitoryStudentBean);
				updateRestBed(dormitoryStudentBean);
			}
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("编辑学生关联宿舍出现异常", e);
		}
		return result;
	}
	
	public void updateRestBed(DormitoryStudentBean dormitoryStudentBean){
		DormitorysBean dormitoryBean = new DormitorysBean();
		DormitorysBean dormitorysBean = dormitoryService.getDormitoryById(dormitoryStudentBean.getDorId());
		int count = dormitoryStudentService.getStudentCountByDorId(dormitoryStudentBean.getDorId());//该宿舍已住的人数
		dormitoryBean.setDorId(dormitoryStudentBean.getDorId());
		dormitoryBean.setRestBed(dormitorysBean.getSumBed()-count);
		dormitoryService.editDormitory(dormitoryBean);
	}

	@Override
	public CommonResult removeDormitoryStudent(Integer stuId,Integer dorId) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			success = dormitoryStudentService.removeDormitoryStudent(stuId,dorId);
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("删除宿舍关联学生出现异常", e);
		}
		return result;
	}

	@Override
	public List<DormitoryStudentBean> getAllDirmitoryStudent(DormitoryStudentExample example) {
		List<DormitoryStudentBean> list = new ArrayList<DormitoryStudentBean>();
		try {
			list = dormitoryStudentService.getAllDirmitoryStudent(example);
		} catch (Exception e) {
			logger.error("查询全部学生关联宿舍出现异常", e);
		}
		return list;
	}

	@Override
	public List<DormitoryStudentBean> queryDormitoryRelateStudentById(Integer id) {
		try {
			return dormitoryStudentService.getDormitoryRelateStudentById(id);
		} catch (Exception e) {
			logger.error("查询宿舍学生列表异常", e);
			return null;
		}
	}

	@Override
	public int queryStudentCountByDorId(Integer dorId) {
		try {
			return dormitoryStudentService.getStudentCountByDorId(dorId);
		} catch (Exception e) {
			logger.error("查询宿舍学生数异常", e);
			return 0;
		}
	}

	@Override
	public DormitoryStudentBean queryDormitoryRelateStudentByStuId(Integer stuId) {
		try {
			return dormitoryStudentService.getDormitoryRelateStudentByStuId(stuId);
		} catch (Exception e) {
			logger.error("根据学生编号查询宿舍学生异常", e);
			return null;
		}
	}

	@Override
	public CommonResult removeDormitoryStudentByStatus(Integer stuId, Integer status) {
		CommonResult result = new CommonResult();
		boolean success= false;
		try {
			success = dormitoryStudentService.removeDormitoryStudentByStatus(stuId, status);
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("通过状态删除宿舍关联学生出现异常", e);
		}
		return result;
	}

	@Override
	public void queryDormitoryByPage(CommonPage commonPage) {
		try {
			dormitoryStudentService.getMap(commonPage);
		} catch (Exception e) {
			logger.error("学生关联宿舍列表异常", e);
		}
	}

	@Override
	public DormitoryStudentBean queryDorStudentByStuIdAndDor(Integer stuId, Integer dorId, Integer stayStatus) {
		try {
			return dormitoryStudentService.getDorStudentByStuIdAndDor(stuId,dorId,stayStatus);
		} catch (Exception e) {
			logger.error("条件查询学生关联宿舍异常", e);
			return null;
		}
	}
}
